//
//  ZJSCSynopsisTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJSCSynopsisTableViewCell.h"
#import "CustomLabel.h"
@interface ZJSCSynopsisTableViewCell ()
/** 课程名字 */
@property (nonatomic, strong) UILabel *name;
/** 主持人头像 */
@property (nonatomic, strong) UIImageView *teacherHeader;
/** 介绍 */
@property (nonatomic, strong) UILabel *introduce;

/** 收缩展开按钮 */
@property (nonatomic, strong) UIButton *spreadButton;
@end

@implementation ZJSCSynopsisTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.name];
        [self.contentView addSubview:self.introduce];
        [self.contentView addSubview:self.spreadButton];
        [self.contentView addSubview:self.teacherHeader];
        
        self.teacherHeader.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .widthIs(16 * 2.5 *[UIScreen zj_screenWScale])
        .heightIs(9 * 2.5 *[UIScreen zj_screenHScale]);
        
        self.name.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 30 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale])
        .heightIs(10);
        
        self.introduce.sd_layout
        .topSpaceToView(self.name, 5* [UIScreen zj_screenHScale])
        .leftEqualToView(self.name)
        .rightEqualToView(self.name)
        .heightIs(10);
        
        
        self.spreadButton.sd_layout
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .topSpaceToView(self.introduce, 0)
        .heightIs(30);
        

        
        
    }
    return self;
}


#pragma mark - 点击事件
- (void)action_spreadButton:(UIButton *)sender{

        if (self.isSynopsisIntroduce) {//课程简介
            if (!self.model.synopsisIntroduce_isUnfold) {//展开
                [[NSNotificationCenter defaultCenter] postNotificationName:INTRODUCECELL_RELOAD object:nil userInfo:@{SynopsisIntroduce:@"1"}];
            }else{//收缩
                [[NSNotificationCenter defaultCenter] postNotificationName:INTRODUCECELL_RELOAD object:nil userInfo:@{SynopsisIntroduce:@"0"}];
            }
            
        }else{//教师简介
            if (!self.model.teacherIntroduce_isUnfold) {//展开
                [[NSNotificationCenter defaultCenter] postNotificationName:INTRODUCECELL_RELOAD object:nil userInfo:@{teachIntroduce:@"1"}];
            }else{//收缩
                [[NSNotificationCenter defaultCenter] postNotificationName:INTRODUCECELL_RELOAD object:nil userInfo:@{teachIntroduce:@"0"}];
            }
        }
        
    sender.selected = !sender.selected;
    
}


#pragma mark - Setter


- (void)setModel:(ZJSCSynopsisModel *)model{
    _model = model;
    if (self.isSynopsisIntroduce) {//是课程简介
        self.teacherHeader.hidden = YES;
        self.name.textAlignment = NSTextAlignmentCenter;
        self.name.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 30 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale])
        .heightIs([NSString zj_sizeOfString:model.COURSEWARECOMMENT.NAME font:self.name.font width:self.name.width].height);
        [self.name updateLayout];
        
        self.introduce.text = model.COURSEWARECOMMENT.COMMENT ? model.COURSEWARECOMMENT.COMMENT : @"";
        //计算字体行数
        NSInteger number = [NSString getLineNum:self.introduce.text font:self.introduce.font labelWidth:self.introduce.width];
        if (number >= 4) {//有展开收缩的情况
             self.spreadButton.hidden = NO;
            if (model.synopsisIntroduce_isUnfold) {//展开
                self.introduce.numberOfLines = 0;
                self.spreadButton.selected = YES;
            }else{//收缩
                self.introduce.numberOfLines = 4;
                self.spreadButton.selected = NO;
            }
        }else{//没有展开收缩的情况
            self.introduce.numberOfLines = 0;
            self.spreadButton.hidden = YES;
    
        }
        
        self.introduce.sd_layout
        .topSpaceToView(self.name, 5* [UIScreen zj_screenHScale])
        .leftEqualToView(self.name)
        .widthIs([UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale])
//        .heightIs([NSString zj_sizeOfString:model.COURSEWARECOMMENT.COMMENT font:self.introduce.font width:self.introduce.width].height)
        .heightIs([self.introduce getLableSizeWithMaxWidth:self.introduce.width].height);
        [self.introduce updateLayout];
        
        self.name.text = model.COURSEWARECOMMENT.NAME;
        
        
    }else{//主持人介绍
        self.teacherHeader.hidden = NO;
        [self.teacherHeader sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,model.TEACHAR_MODEL.IMAGEPHOTO_THUMBURL]] placeholderImage:PlaceholderImage];
        self.name.text = model.TEACHAR_MODEL.NAME;
        
        self.name.textAlignment = NSTextAlignmentLeft;
        self.name.numberOfLines = 1;
        self.name.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.teacherHeader, 10 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - (24 + 16 * 2.5 + 10) * [UIScreen zj_screenWScale])
        .heightIs(9 * 2.5 * [UIScreen zj_screenHScale]);
        [self.name updateLayout];
        
         self.introduce.text = model.TEACHAR_MODEL.COMMENT_NOTAG ? model.TEACHAR_MODEL.COMMENT_NOTAG : @"";
        
        //计算字体行数
        NSInteger number = [NSString getLineNum:self.introduce.text font:self.introduce.font labelWidth:self.introduce.width];
        if (number >= 4) {//有展开收缩的情况
            self.spreadButton.hidden = NO;
            if (model.teacherIntroduce_isUnfold) {//展开
                self.introduce.numberOfLines = 0;
                self.spreadButton.selected = YES;
            }else{//收缩
                self.introduce.numberOfLines = 4;
                self.spreadButton.selected = NO;
            }
        }else{//没有展开收缩的情况
            self.introduce.numberOfLines = 0;
            self.spreadButton.hidden = YES;
            
        }
        self.introduce.sd_layout
        .topSpaceToView(self.name, 5* [UIScreen zj_screenHScale])
        .leftEqualToView(self.name)
        .widthIs([UIScreen zj_screenWith] - (24 + 16 * 2.5 + 10) * [UIScreen zj_screenWScale])
//        .heightIs([NSString zj_sizeOfString:model.TEACHAR_MODEL.COMMENT_NOTAG font:self.introduce.font width:self.introduce.width].height)
        .heightIs([self.introduce getLableSizeWithMaxWidth:self.introduce.width].height);
    
        [self.introduce updateLayout];
    
       
    }
}


#pragma mark - Getters
- (UILabel *)name{
    if (_name == nil){
        _name = [UILabel new];
        _name.textColor = [UIColor textBlackColor];
        _name.font = [UIFont boldSystemFontOfSize:15 * [UIScreen zj_textFontScale]];
        _name.textAlignment = NSTextAlignmentCenter;
        _name.numberOfLines = 0;
    }
    return _name;
}

- (UIImageView *)teacherHeader{
    if (_teacherHeader == nil){
        _teacherHeader = [UIImageView new];
        _teacherHeader.contentMode = UIViewContentModeScaleToFill;
//        _teacherHeader.backgroundColor = [UIColor themeColor];
    }
    return _teacherHeader;
}

- (UILabel *)introduce{
    if (_introduce == nil){
        _introduce = [UILabel new];
        _introduce.textColor = [UIColor textBlackColor];
        _introduce.font = [UIFont titleTextFont];
        _introduce.characterSpace = 1;
        _introduce.lineSpace = 4 * [UIScreen zj_screenWScale];
        _introduce.numberOfLines = 0;
    }
    return _introduce;
}


- (UIButton *)spreadButton{
    if (_spreadButton == nil){
        _spreadButton = [[UIButton alloc] init];
        [_spreadButton setTitle:@"点击展开" forState:UIControlStateNormal];
        [_spreadButton setTitle:@"点击收缩" forState:UIControlStateSelected];
        [_spreadButton setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        _spreadButton.titleLabel.font = self.introduce.font;
        [_spreadButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [_spreadButton setImage:[UIImage imageNamed:@""] forState:UIControlStateSelected];
        [_spreadButton addTarget:self action:@selector(action_spreadButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _spreadButton;
}

@end
